package com.atguigu.gulimall.order.listener;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 监听关闭订单
 * @Author 小坏
 * @Date 2021/2/17 19:41
 * @Version 1.0
 * @program: 父工程 gulimall 万物起源之地
 */

@RabbitListener(queues = "order.release.order.queue")
@Service
public class OrderCloseListener {

    @Autowired
    private OrderService orderService;

    //订单创建成功就发消息
    @RabbitHandler
    public void listener(OrderEntity orderEntity, Channel channel, Message message) throws IOException {
        System.out.println ("收到过期的信息、准备关闭订单" + orderEntity.getOrderSn ());
        try {
            orderService.closeOrder (orderEntity);
            //手动调用支付宝的收单功能--->在线文档https://opendocs.alipay.com/apis/api_1/alipay.trade.close
            channel.basicAck (message.getMessageProperties ().getDeliveryTag (), false);
        } catch (Exception e) {
            //消息已拒绝、还返回到队列里面
            channel.basicReject (message.getMessageProperties ().getDeliveryTag (), true);
        }
    }
}
